<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>

<jsp:include page="/WEB-INF/views/layouts/header.jsp" />
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
// 网站地址
String netPath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort();
response.setHeader("Pragma","No-cache");  
response.setHeader("Cache-Control","no-cache");  
response.setDateHeader("Expires",0);
%>
<!-- 双日历插件 -->
<script type="text/javascript"
	src="<%=basePath%>js/jquery-ui-1.7.1.custom.min.js"></script>
<script type="text/javascript"
	src="<%=basePath%>js/daterangepicker.jQuery.js"></script>

<link rel="stylesheet" href="<%=basePath%>css/ui.daterangepicker.css"
	type="text/css" />
<link rel="stylesheet"
	href="<%=basePath%>css/redmond/jquery-ui-1.7.1.custom.css" type="text/css"
	title="ui-theme" />
<style>
.yhgltable ul li span.current {
	font-weight: bold
}

.chooseColumn {
	width: 196px;
	height: 30px;
	line-height: 30px;
	color: #666;
	border: 1px solid #BBB;
}

.chooseColumn option {
	height: 30px;
	min-width:196px;
}

.next,.prev {
	filter: Alpha(opacity =50);
	-moz-opacity: .1;
	opacity: 0.5;
}
</style>
<script type="text/javascript">
	var sbReturnMessage = "${returnMessage}";
	// 搜索
	var searchText = "";
	// 选择的栏目
	var choiceCol=-1;
	// 选择的时期
	var startDate = ""; 
	var endDate = "";
	// 页码
	var pageNo = 1;

	// 总页数
	var pageSum = 1;

	//双日历插件
	$(function(){
		  $('#rangeA').daterangepicker({
			presetRanges:[
				 {text: 'My Range', dateStart: 'Today', dateEnd: 'Today' }
			],
			presets:{
				dateRange:'自定义时间'
			},
			rangeStartTitle:'起始日期',
			rangeEndTitle:'结束日期',
			nextLinkText:'下月',
			prevLinkText:'上月',
			doneButtonText:'确定',
			cancelButtonText:'取消',
			earliestDate:'',
			latestDate:Date.parse('Today'),
			constrainDates:true,
			rangeSplitter:'-',
			dateFormat:'yy-mm-dd',
			closeOnSelect:false,
			onClose: function(){
				pageNo = 1;
				refreshArticles();
			},
			onOpen:function(){
				$('.ui-daterangepicker-dateRange').click().parent().hide();
			}}
		); 
		
	// 弹出错误提示窗口
	if(sbReturnMessage!=""){
		alert(sbReturnMessage);
	}
	
	// 防止从缓存中读取数据
	$.ajaxSetup ({ cache: false });
});
	
	// 默认翻页是对于filterArticles结果集来说
	function initPageButton(){
		
		if(pageSum>1){
			if(pageNo==1){
				//默认不可用
				$("[name=prevButton]").addClass("prev")
				$("[name=nextButton]").removeClass("next");
			} else if(pageNo==pageSum){
				//下翻页不可用
				$("[name=nextButton]").addClass("next")
				$("[name=prevButton]").removeClass("prev");
				//alert($("[name=prevButton]").attr("class"));
			} else {
				// 上下翻页都可用
				$("[name=prevButton]").removeClass("prev");
				$("[name=nextButton]").removeClass("next");
				
			}
		} else {
			$("[name=prevButton]").addClass("prev");
			$("[name=nextButton]").addClass("next");
		}
	};
	
	// 局部刷新
	function refreshArticles(){

	    if(searchText==""){
	    	searchText = $("[name=searchText]").val()?encodeURIComponent($("[name=searchText]").val()):"";
	    }
		var timeValue = $("#rangeA").val();
		if(timeValue.length==10){
			startDate = "";
			endDate = timeValue
		} else if(timeValue.length==23){
			startDate = timeValue.substring(0,10);
			endDate = timeValue.substring(13,23);
		} else {
			$("#rangeA").val("");
			startDate = "";
			endDate = "";
		}

		// 获取栏目
		setColumn();
		$.ajaxSetup ({ cache: false });
		//alert("searchText:" + searchText +"  choiceCol:"+choiceCol+"  pageNo" + pageNo);
		$(".userContent").load("<%=basePath%>article/filterArticles/0?searchText="+searchText+"&startDate="+startDate
		+"&endDate="+endDate+"&choiceCol="+choiceCol+"&pageNo="+pageNo,function(response,status){
      		if (status=="success"){
      			var intPageSum = parseInt($(response).find("input[name='pageSum']").val());
      			if(intPageSum>=1){
      			pageSum = intPageSum;
      			}
      			initPageButton();
      		}
        });
	};
	
	// 栏目选择即时刷新
	function filterColumn(){
		pageNo = 1;
		refreshArticles();
	};
	
	// 获得栏目id
	function setColumn(){
	
		// 最后一个
		var seletedCol = $("select[name=chooseColumn] option[selected]:last");
		var colID = seletedCol.val();
		if(colID==undefined){
			choiceCol = -1;
		} else {
			choiceCol = colID;
		}
	};
	
 	function toInsert(){
		document.location="<%=basePath%>article/add"
	};

	/**
	 *把指定的一组checkbox所选的值构造为逗号分隔的字符串
	 */
	function getAllValue(group_box) {
		var boxes = document.getElementsByName(group_box);
		var values = "";
		for (var i = 0; i < boxes.length; i++) {
			if (boxes[i].checked) {
				values += boxes[i].value + ",";
			}
		}
		if (values.length > 0) {
			values = values.substring(0, values.length - 1);
		}
		return values;
	};

	function putIntoRecycleBin(){
		var operatorIds = getAllValue("group_box");
		if(!operatorIds){
			alert("请选择需要放入回收站的文章！");
			return;
		}
		if(confirm("确认放入回收站？")){
			$.ajax({
			  url: "<%=basePath%>article/toRecycleBin",
			  type:"POST",
			  contentType:"application/x-www-form-urlencoded; charset=utf-8",
			  data: "operatorIds="+operatorIds,
			  dataType: 'text',
			  success: function(data) {
				 var jsonobj=eval('('+data+')');
				 if(jsonobj.state==='1'){
					refreshArticles();
					alert(jsonobj.result);
				 } else if(jsonobj.state==='0'){
					 alert(jsonobj.result);
				 }
			  },
			  error: function(XMLHttpRequest, textStatus, errorThrown) {
					 alert("放入回收站失败！");
			  }
			});
		}
	};
	
	function viewRecycleBin(){
		document.location="<%=basePath%>article/recycleBin"
	};
	function toUpdate(newsID){
		window.location="<%=basePath%>article/update/" + newsID;
	};
	function search(){
		pageNo = 1;
		pageSum = 1;
		searchText = $("[name=searchText]").val()?encodeURIComponent($("[name=searchText]").val()):"";
		// 让ie不读取缓存
		$.ajaxSetup ({ cache: false });
		$(".userContent").load("<%=basePath%>article/search/0?searchText="+searchText+"&rd="+Math.random()+"&pageNo="+pageNo,function(response,status){
      		if (status=="success"){
      			var intPageSum = parseInt($(response).find("input[name='pageSum']").val());
      			//alert(intPageSum);
      			if(intPageSum>=1){
      				pageSum = intPageSum;
      			}
      			initPageButton();
      			//alert("pageNo:"+pageNo+":pageSum:"+pageSum);
      		}
        });
	};
	
	// 下一页
	function nextPage(){
		if(pageNo<pageSum){
			pageNo ++;
			initPageButton();
			//alert("pageNo:" + pageNo + "  pageSum:" + pageSum);
			refreshArticles()
		}
		//alert(pageNo);
	};
	
	function prevPage(){
		
		if(pageNo>1){
			pageNo --;
			initPageButton();
			refreshArticles()
		}
		//alert(pageNo);
	};
	
	// 最高级别是3，最低级别是0（0：不置顶）
	function makeTop(tid){
		var iftop = "#iftop_" + tid;
		var topValue = parseInt($(iftop).val());
		if(topValue<3){
			topValue ++;
			//$(iftop).val(topValue);
			changeTopStat(tid,topValue);
		}
	};
	// 置顶操作
	function makeLower(tid){
		var iftop = "#iftop_" + tid;
		var topValue = parseInt($(iftop).val());
		if(topValue>0){
			topValue --;
			//$(iftop).val(topValue);
			changeTopStat(tid,topValue);
		}
	};

	function changeTopStat(tid,topValue){
		$.ajax({
			  url: "<%=basePath%>article/makeTop",
			  type:"POST",
			  contentType:"application/x-www-form-urlencoded; charset=utf-8",
			  data: {"id":tid,"iftop":topValue},
			  dataType: 'text',
			  success: function(data) {
				 var jsonobj=eval('('+data+')');
				 if(jsonobj.state==='1'){
					var iftop = "#iftop_" + tid;
					$(iftop).val(topValue);
					alert(jsonobj.result);
				 } else if(jsonobj.state==='0'){
					 alert(jsonobj.result);
				 }
			  },
			  error: function(XMLHttpRequest, textStatus, errorThrown) {
					 alert("修改失败！");
			  }
		});
	};

  //推荐
  function changeRecommendStat(tid){

	  var recommendItem = "#recommendItem_" + tid;
	  var recommendImg = "#recommendImg_" + tid;
	  var recommendValue =  $(recommendItem).val();

	  // 取消推荐
	  if(recommendValue == 1){
		  recommendValue = 0;
		  $(recommendImg).attr("src","<%=basePath%>images/commends0.png");
		  $(recommendImg).attr("title","推荐");
		  //$(recommendItem).val(recommendValue);
	  } else {
	  // 推荐
		  recommendValue = 1;
		  $(recommendImg).attr("src","<%=basePath%>images/commends1.png");
		  $(recommendImg).attr("title","取消推荐");
		  //$(recommendItem).val(recommendValue);
	  }

	  $.ajax({
		  url: "<%=basePath%>article/recommend",
		  type:"POST",
		  contentType:"application/x-www-form-urlencoded; charset=utf-8",
		  data: {"id":tid,"recommendValue":recommendValue},
		  dataType: 'text',
		  success: function(data) {
			 var jsonobj=eval('('+data+')');
			 if(jsonobj.state==='1'){
				$(recommendItem).val(recommendValue);
				alert(jsonobj.result);
			 } else if(jsonobj.state==='0'){
				 alert(jsonobj.result);
			 }
		  },
		  error: function(XMLHttpRequest, textStatus, errorThrown) {
				 alert("修改失败！");
		  }
		});

 	};
 
</script>
<!--中间部分开始-->
<div class="main">
	<!-- 左侧 -->
	<jsp:include page="/WEB-INF/views/article/sidebar.jsp" />
	<!-- /左侧 -->
	<!-- 核心内容区域 -->
	<div id="iaas_layout_content" class="rightblock" >
		<p class="shadow"></p>
		<div class="content">
			<!--默认状态下引用样式ml60，展开后引用样式ml140-->
			<div class="mainblock">
				<div class="tabA">
					<h1>
						<a href="<%=basePath%>article/operate" style="color:#585858">文章管理</a>
					</h1>
				</div>
				<div class="lineone mt10">
					<span> <img name="prevButton" src="<%=basePath%>images/back.png"
						class="prev linkhand" style="cursor:pointer;"
						onclick="prevPage();"> <img name="nextButton"
						src="<%=basePath%>images/next.png" class="next linkhand"
						style="cursor:pointer;" onclick="nextPage();" /> </span>
					<div class="btn">
						<a href="javascript:void(0)" onclick="toInsert()"><img
							src="<%=basePath%>images/xz.gif" />新增</a> <a href="javascript:void(0)"
							onclick="putIntoRecycleBin()"><img
							src="<%=basePath%>images/del.gif" />放入回收站</a>
							<a href="javascript:void(0)"
							onclick="viewRecycleBin()"><img
							src="<%=basePath%>images/dustbin.gif" />查看回收站</a>
					</div>
					<div class="search">
						<input type="text" name="searchText" style="color:#666"
							placeholder="请输入文章标题查询" onblur="search()"/> <img src="<%=basePath%>images/search.png"
							onclick="search()" style="cursor: pointer" />
					</div>
					
					<div style="margin-left: 10px;border:1px solid #BBB;" class="search">
						<!-- 起始时间--结束时间 -->
						<input type="text" title="请点击选定时间段" id="rangeA"
							style="height:30px;display:block;border: 0px;" />
						<img src="<%=basePath%>images/choose_date.png" style="margin-right:10px;" />
					</div>

					<div style="height: 30px;float:left;margin-left:10px;border:0 none;padding: 0;">
						<!-- 栏目   -->
						<select name="chooseColumn" class="chooseColumn"
							onchange="filterColumn()">
							<option value="-1">全部栏目</option> ${downSelectCol}
						</select>
					</div>
				</div>
				
					<table border="0" cellpadding="0" cellspacing="0"
						class="yhgltable mt10" style="width:100%;">
						<tr>
							<td rowspan="2" class="userContent" valign="top">
							<script type="text/javascript" language="javascript">
									// 禁止ie读取缓存
									$.ajaxSetup ({ 
									cache: false
									 });
				            		$(".userContent").load("<%=basePath%>article/showArticles",function(response,status){
				            			if (status=="success"){
				            				var intPageSum = parseInt($(response).find("input[name='pageSum']").val());
				            				if(intPageSum>=1){
				            					pageSum = intPageSum;
				            				}
				            				initPageButton();
				            			}
				            		});
				            		//alert("pageSum:" + pageSum);
				            		//initPageButton();
				            	</script>
				            </td>
						</tr>
					</table>
			
			</div>
		</div>
	</div>
</div>
<!--中间部分结束-->
<!--底部开始-->
<jsp:include page="/WEB-INF/views/layouts/footer.jsp" />
<!--底部结束-->
</body>
</html>
